<template>
    <div>
        <h1 v-show="isSearch">请搜索</h1>
        <h1 v-show="isLoading">Loading</h1>
        <ul class="user-view-container">
            <li v-for="item in users" :key="item.id">
                <a :href="item.html_url">
                    <img :src="item.avatar_url"/>
                    <p>{{item.login}}</p>
                </a>
            </li>
        </ul>
    </div>
</template>

<script>
import axios from 'axios';
export default {
    name: "SearchUser",
    created(){
        this.$EventBus.$on('search',this.search);
    },
    beforeDestroy(){
        this.$EventBus.$off('search');
    },
    data(){
        return {
            users: [],
            isSearch: true,
            isLoading:false
        }
    },
    methods: {
        async search(searchName){
            this.isSearch = false;
            this.isLoading = true;
            const res = await axios({
				method: "GET",
				url: `https://api.github.com/search/users`,
				params: {
					q: searchName,
				}
			});
            console.log(res.data.items);
            this.users = res.data.items;
            this.isLoading = false;
        }
    }
}
</script>

<style>
a{
    text-decoration: none;
    color: black;
}
.user-view-container{
    display: flex;
    flex-wrap: wrap;
    justify-content:flex-start;
}
.user-view-container li{
    text-align: center;
    list-style: none;
    margin: 15px 0;
    width: 25%;
    border: 1px solid #ccc;
    box-sizing: border-box;
}
.user-view-container li img{
    width: 80%;
    height: 80%;
}
             
</style>